//
//  SYOthersTVCell.m
//  TermOfSectionB
//
//  Created by 韩震明 on 15/11/30.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "SYOthersTVCell.h"

@implementation SYOthersTVCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}

- (void)createCell
{
    self.headImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.headImageView];
    
    self.userImageView = [[UIImageView alloc] init];
    [self.headImageView addSubview:self.userImageView];
    self.userImageView.layer.cornerRadius = 20;
    self.userImageView.layer.masksToBounds = YES;
    
    self.userLabel = [[UILabel alloc] init];
    [self.headImageView addSubview:self.userLabel];
    self.userLabel.font = [UIFont systemFontOfSize:13.0];
    
    self.timeLabel = [[UILabel alloc] init];
    [self.headImageView addSubview:self.timeLabel];
    self.timeLabel.font = [UIFont systemFontOfSize:11.0];
    self.timeLabel.textColor = [UIColor grayColor];
    
    self.picImageView = [[UIImageView alloc] init];
    [self.headImageView addSubview:self.picImageView];
    
    self.contentLabel = [[UILabel alloc] init];
    [self.headImageView addSubview:self.contentLabel];
    self.contentLabel.textColor = [UIColor grayColor];
    self.contentLabel.font = [UIFont systemFontOfSize:15.0];
    self.contentLabel.numberOfLines = 0;
    [self.contentLabel sizeToFit];
    
    
    self.footView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.footView];
    self.footView.userInteractionEnabled = YES;
    
    self.shareImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"share.png"]];
    [self.footView addSubview:self.shareImageView];
    
    self.commentImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Comment.png"]];
    [self.footView addSubview:self.commentImageView];
    
    [self.footView addSubview:self.likesButton];
    
    self.shareImageView.backgroundColor = [UIColor redColor];
    self.commentImageView.backgroundColor= [UIColor yellowColor];
    [self.likesButton setBackgroundColor:[UIColor blackColor]];
}

- (UIButton *)likesButton
{
    if (!_likesButton) {
        self.likesButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_likesButton setImage:[UIImage imageNamed:@"No_Attention"] forState:UIControlStateNormal];
        [_likesButton addTarget:self action:@selector(cellBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _likesButton;
}


- (void)cellBtnClicked:(UIButton *)likeButton
{
    _cellModel.liked = !_cellModel.liked;
    if (_cellModel.liked) {
        [_likesButton setImage:[UIImage imageNamed:@"Pay_Attention"] forState:UIControlStateNormal];
        [SaveTool saveListToPlist:self.cellModel];
        
    } else {
        [_likesButton setImage:[UIImage imageNamed:@"No_Attention"] forState:UIControlStateNormal];
        [SaveTool cancelListInPlist:self.cellModel];
    }
}


- (void)reload
{
    if ([SaveTool ishaveCollected:self.cellModel]) {
        [self.likesButton setImage:[UIImage imageNamed:@"No_Attention"] forState:UIControlStateNormal];
        self.cellModel.liked = NO;
    }
    else {
        self.cellModel.liked = YES;
        [self.likesButton setImage:[UIImage imageNamed:@"Pay_Attention"] forState:UIControlStateNormal];
    }
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:_authorModel.avatar]];
    self.userLabel.text = _authorModel.username;
    self.timeLabel.text = _cellModel.datestr;
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:_picModel.picsUrl]];
    self.contentLabel.text = _cellModel.content;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self reload];
    
    [self.headImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.contentView);
        make.top.equalTo(self.contentView).with.offset(10);
        make.bottom.equalTo(self.footView.top).with.offset(- 5);
    }];
    
    [self.userImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headImageView).with.offset(5);
        make.left.equalTo(self.headImageView).with.offset(3);
        make.width.height.equalTo(40);
    }];
    
    [self.userLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.userImageView.right).with.offset(3);
        make.top.equalTo(self.headImageView).with.offset(5);
        make.width.equalTo(200);
        make.height.equalTo(20);
    }];
    
    [self.timeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userLabel.bottom);
        make.left.equalTo(self.userLabel);
        make.width.height.equalTo(self.userLabel);
    }];
    
    [self.picImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userImageView.bottom).with.offset(10);
        make.left.and.right.equalTo(self.headImageView);
        make.bottom.equalTo(self.contentLabel.top).with.offset(- 5);
    }];
    
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView).with.offset(10);
        make.right.equalTo(self.headImageView).with.offset(- 5);
        make.bottom.equalTo(self.headImageView).with.offset(- 10);
    }];
    
    
    
    [self.footView makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).with.offset(- 10);
        make.height.equalTo(40);
    }];
    
    [self.shareImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.and.bottom.equalTo(self.footView);
        make.right.equalTo(self.commentImageView.left);
    }];
    
    [self.commentImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.footView);
        make.width.equalTo(self.shareImageView);
        make.right.equalTo(self.likesButton.left);
    }];
    
    [self.likesButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.and.right.equalTo(self.footView);
        make.width.equalTo(self.shareImageView);
    }];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
